package org.zch.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zch.common.utils.IpUtils;
import org.zch.dao.CommentDAO;
import org.zch.entity.dto.CommentDTO;
import org.zch.entity.po.CommentPO;
import org.zch.entity.po.MessagePO;
import org.zch.entity.po.WorksPO;
import org.zch.entity.vo.CommentVO;
import org.zch.entity.vo.UserInfoVO;
import org.zch.service.CommentService;
import org.zch.service.MessageService;
import org.zch.service.UserService;
import org.zch.service.WorksService;

import java.util.Date;
import java.util.List;

/**
 * 评论表(Comment)服务实现类
 *
 * @author 刘新
 * @since 2024-01-11 16:39:55
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDAO, CommentPO> implements CommentService {

    @Autowired
    private UserService userService;

    @Autowired
    private CommentDAO commentDAO;
    @Autowired
    private MessageService msgService;
    @Autowired
    private WorksService worksService;

    /**
     * 发表评论
     *
     * @param commentPO 实体对象
     * @return 新增结果
     */
    @Override
    public CommentVO insert(CommentPO commentPO) {
        // 新增评论
        UserInfoVO infoVO = userService.getByCode(commentPO.getUserCode());
        commentPO.setCreateTime(new Date());
        commentPO.setUserId(infoVO.getId());
        // 解析ip
        String location = this.getIp(commentPO.getLocation());
        commentPO.setLocation(location);
        int i = commentDAO.insert(commentPO);
        // 作品信息
        WorksPO worksPO = worksService.getById(commentPO.getWorksId());
        //不是本人评论本人时，发送消息
        if (!infoVO.getId().equals(worksPO.getUserId())){
            //发送消息
            MessagePO msgPO = new MessagePO();
            msgPO.setMsgContent(infoVO.getName() + "评论了：" + commentPO.getContent());
            msgPO.setSendUserId(infoVO.getId());
            msgPO.setSendUserCode(infoVO.getCode());
            msgPO.setToUserId(worksPO.getUserId());
            msgPO.setWorkId(worksPO.getId());
            msgPO.setType("JUMP_INDEX");
            msgPO.setValue(worksPO.getId().toString());
            msgPO.setToUserCode(worksPO.getUserCode());
            msgPO.setRelateId(commentPO.getId());
            msgPO.setSource(1);
            msgService.sendMsg(msgPO);
        }
        // 返回插入的数据
        return this.getCommentById(commentPO.getId());
    }

    /**
     * 根据作品id，查询评论
     *
     * @param dto 查询条件
     * @return 评论列表
     */
    @Override
    public IPage<CommentVO> getComment(CommentDTO dto) {
        return commentDAO.getComment(dto);
    }


    /**
     * IP 自定义内容
     *
     * @param ipLocation
     * @return
     */
    public String getIp(String ipLocation) {
        if (null == ipLocation) {
            return "未知星球";
        }
        String location = "";
        String province = IpUtils.getProvince(ipLocation);
        if (null != province) {
            location = location + province;
        } else {
            return "未知星球";
        }
        return location;
    }

    /**
     * 根据主键id，查询评论
     *
     * @param id 查询条件
     * @return 评论列表
     */
    public CommentVO getCommentById(Integer id) {
        CommentDTO dto = new CommentDTO();
        dto.setId(id);
        IPage<CommentVO> page = commentDAO.getComment(dto);
        List<CommentVO> list = page.getRecords();
        if (ArrayUtil.isAllNotEmpty(list)) {
            return list.get(0);
        } else {
            return new CommentVO();
        }
    }

}

